﻿Imports System.Reflection
Imports System.Resources
Imports System.Globalization

Imports DTO
Imports BUS

Public Class frmTheLoai

    ReadOnly culInfoMgr As CommonLibrary

    Public Sub New(ByVal culInfoMgr As CommonLibrary)
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.culInfoMgr = culInfoMgr
        AddHandler culInfoMgr.OnCultureInfoChanges, AddressOf CultureInfiHasChange
    End Sub

    Private Sub CultureInfiHasChange(ByVal sender As Object, ByVal e As EventArgs)
        Try
            SetUI()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub SetUI()
        Dim culInfo As CultureInfo = culInfoMgr.getCultureinfo
        Dim rm As New ResourceManager("Gui.frmTheLoai", GetType(frmThemSachCu).Assembly)
        Me.Text = rm.GetString("frmTheLoaiText", culInfo)
        lblTheLoai.Text = rm.GetString("lblTheLoaiText", culInfo)
        btnThemTheLoai.Text = rm.GetString("btnThemTheLoaiText", culInfo)
        btnXoa.Text = rm.GetString("btnXoaText", culInfo)
    End Sub

    Private Sub btnThemTheLoai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemTheLoai.Click

        If KiemTra() = False Then
            Exit Sub ' ' ' 'không cho ô thể loại rỗng
        End If

        Try
            Dim theloai As New TheLoaiBUS
            Dim tl As New TheLoaiDTO
            tl.LayTenTheLoai = LCase(txtTheLoai.Text.Trim)
            theloai.ThemTheLoai(tl)
            ThanhCong()
            Me.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Public Sub ThanhCong()
        Dim ngon As CultureInfo = culInfoMgr.getCultureinfo
        If ngon.Name = "vi-VN" Then
            MessageBox.Show("Thêm Thành Công.", "Thông Báo", MessageBoxButtons.OK)
        Else
            MessageBox.Show("Add Success.", "Success", MessageBoxButtons.OK)
        End If

    End Sub


    Dim loai As Integer = frmLogin.loai

    Public Sub LoadSize()
        If loai = 1 Then
            Me.Width = 419
            Me.Height = 125
            btnXoa.Visible = False
        Else
            Me.Width = 491
            Me.Height = 390
        End If
    End Sub

    Private Sub frmTheLoai_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            SetUI()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        LoadSize()
        LoadTheLoai()
    End Sub

    Public Function KiemTra() As Boolean

        Dim ngon As CultureInfo = culInfoMgr.getCultureinfo
        If ngon.Name = "vi-VN" Then
            If txtTheLoai.Text = "" Then
                MessageBox.Show("Thể loại không được bỏ trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If
        Else
            If txtTheLoai.Text = "" Then
                MessageBox.Show("Category is not empty", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If
        End If
        Return True

    End Function

    Public Function HoiXoa() As DialogResult
        Dim xoa As DialogResult
        Dim NgonNguHientien As CultureInfo = culInfoMgr.getCultureinfo
        If NgonNguHientien.Name = "vi-VN" Then
            xoa = MessageBox.Show("Bạn Muốn Xóa ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            Return xoa
        Else
            xoa = MessageBox.Show(" Are You Delete .", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            Return xoa
        End If
    End Function

    Public Sub XoaThanhCong()
        Dim NgonNguHientien As CultureInfo = culInfoMgr.getCultureinfo
        If NgonNguHientien.Name = "vi-VN" Then
            MessageBox.Show("Xóa Thành Công.", "Thông Báo", MessageBoxButtons.OK)
        Else
            MessageBox.Show("Delete Success.", "Success", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If DataGridView_TheLoai.SelectedRows.Count = 0 Then
            frmMain.Xoaloi()
            Exit Sub
        End If
        Try
            ' ' ' xóa thể loại phải xoa 4 bang ChitTietSach , ChiTietHoaDon, Sach, TheLoai
            If HoiXoa() = Windows.Forms.DialogResult.Yes Then

                If (DataGridView_TheLoai.SelectedRows.Count > 0) Then
                    For i = 0 To DataGridView_TheLoai.SelectedRows.Count - 1
                        Dim idTheLoai As New TheLoaiDTO

                        idTheLoai.LayIdTheLoai = DataGridView_TheLoai.SelectedRows(i).Cells(0).Value

                        'Dim cthd As New ChiTietHoaDonBUS ' ' ' 'Xóa Bảng ChiTietHoaDon
                        'cthd.DeleteSach(idTheLoai)

                        'Dim chitietSach As New ChiTietSachBUS '' ' ' ' Xoa Bảng ChiTietSach
                        'chitietSach.DeleteSach_Loai(idTheLoai)

                        'Dim sach As New SachBUS  ' ' ' ' ' 'Xóa bảng Sách
                        'sach.XoaTheloai(idTheLoai)

                        Dim s As New TheLoaiBUS  ' ' '' Xoa bảng Thể Loại
                        s.XoaTheLoai(idTheLoai)

                    Next

                    XoaThanhCong()
                    Dim s1 As New TheLoaiBUS
                    Dim dt As New DataTable
                    dt = s1.LayTheLoai()
                    DataGridView_TheLoai.DataSource = dt
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Sub LoadTheLoai()
        Dim dt As New DataTable
        Dim theloai As New TheLoaiBUS
        dt = theloai.LayTheLoai
        DataGridView_TheLoai.DataSource = dt
    End Sub

End Class